//
//  AppDelegate.m
//  osx
//
//  Created by 郭涧川 on 16/6/27.
//  Copyright © 2016年 郭涧川. All rights reserved.
//
#include <stdio.h>
#include <string.h>

#import "AppDelegate.h"

#include "locketdef.h"
#include "locketcipher.h"
#include "locketaes.h"
#include "locketerrno.h"


@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    
    int iErr = ERR_COMMON_SUCCESS;
    
    /* 初始化 */
    LOCKET_CIPHER_Init();
    
    /* AES 加密 */
    CIPHER_AES_S stCipherAes;
    memset (&stCipherAes, 0, sizeof (stCipherAes));
    
    stCipherAes.pucInput = (unsigned char*)"hello,world";
    stCipherAes.iInputLen = strlen((char*) stCipherAes.pucInput);
    stCipherAes.iInputEncode = ENCODE_BINARY;
    
    unsigned char szbuffer[1024*2]= {0};
    stCipherAes.iMode = ENCRYPT;
    stCipherAes.iOutputEncode = ENCODE_BASE64;
    stCipherAes.pucOutput = szbuffer;
    
    stCipherAes.pucKey = (unsigned char*)"dGhpcyBpcyBrZXk=";
    stCipherAes.pucIv = (unsigned char*)"dGhpcyBpcyBpdg==";
    
    snprintf(stCipherAes.szAlgoName, sizeof (stCipherAes.szAlgoName), "%s", "AES-128-CFB");
    
    iErr = LOCKET_CIPHER_AESCrypt (&stCipherAes);
    if (ERR_COMMON_SUCCESS != iErr)
    {
        printf ("AES encrypt failed, iErr:%d\n", iErr);
        return;
    }

    /* AES 解密 */
    stCipherAes.iMode = DECRYPT;
    stCipherAes.pucInput = stCipherAes.pucOutput;
    stCipherAes.iInputLen = stCipherAes.iOutputLen;
    stCipherAes.iInputEncode = ENCODE_BASE64;
    
   // stCipherAes.pucKey = (unsigned char*)"this is key";
    //stCipherAes.pucIv = (unsigned char*)"this is iv";
    
    unsigned char szbuffer2[1024*2]= {0};
    stCipherAes.pucOutput = szbuffer2;
    stCipherAes.iOutputEncode = ENCODE_BINARY;
    iErr = LOCKET_CIPHER_AESCrypt (&stCipherAes);
    if (ERR_COMMON_SUCCESS != iErr)
    {
        printf ("AES decrypt failed");
        return;
    }

    printf ("finish\n");
    
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
    
    
}

@end
